package tankgame4;

import java.util.Vector;

public class Enemy extends Tank implements Runnable{
    Vector<Shot> shots =new Vector<>();//一个坦克的多个子弹
    boolean isLive=true;

    public Enemy(int x, int y) {
        super(x, y);
    }

    @Override
    public void run() {
        while (true){
            Shot s =null;
            //敌人坦克能发射很多子弹
            if(isLive && shots.size() < 3){
                switch (getDirect()){
                    case 0:
                        s = new Shot(getX()+20,getY(),0);
                        break;
                    case 1:
                        s = new Shot(getX()+60,getY()+20,1);
                        break;
                    case 2:
                        s = new Shot(getX()+20,getY()+60,2);
                        break;
                    case 3:
                        s = new Shot(getX()+20,getY()+20,3);
                        break;
                }
                shots.add(s);
                //启动
                new Thread(s).start();
            }


        //沿着本来的方向，较缓慢的移动
        switch (getDirect()){
            case 0:
                for (int i = 0; i < 30; i++) {
                    if (getY()>0){
                        moveUp();
                    }
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case 1:
                for (int i = 0; i < 30; i++) {
                    if (getX()+60<1000)
                    moveRight();
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case 2:
                for (int i = 0; i < 30; i++) {
                    if(getY()+60<650)
                moveDown();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
                break;
            case 3:
                for (int i = 0; i < 30; i++) {
                    if (getX()>0)
                    moveLeft();
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                break;

        }//switch

        //随机转变方向(0-3)
        setDirect((int)(Math.random()*4));
        //退出这一个地方坦克线程
        if(!isLive){
            break;
        }
        }//while
    }//run
}
